﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Framework
{
    /// <summary>
    /// Microsoft Entreprise Library <see cref="IObjectCache">cache</see> implementation
    /// </summary>
    public class EntLibObjectCache : IObjectCache
    {
        private readonly ICacheManager _Cache;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="cache"><see cref="ICacheManager"/> instance</param>
        /// <exception cref="ArgumentNullException">If <paramref name="cache"/> is <see langref="null"/>.</exception>
        public EntLibObjectCache(ICacheManager cache)
        {
            if (cache == null)
                throw new ArgumentNullException("cache");
            _Cache = cache;
        }

        /// <summary>
        /// Finds the cached object instance
        /// </summary>
        /// <param name="key">cache key</param>
        /// <returns><see langref="object"/> if found else <see langref="null"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langref="null"/> or empty.</exception>
        public object Find(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");
            return _Cache.GetData(key);
        }

        /// <summary>
        /// Adds an object instance to the cache
        /// </summary>
        /// <param name="key">cache key</param>
        /// <param name="instance"><see langref="object"/> instance</param>
        /// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langref="null"/> or empty.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="instance"/> is <see langref="null"/>.</exception>
        public void Add(string key, object instance)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");
            if (instance == null)
                throw new ArgumentNullException("instance");
            _Cache.Add(key, instance);
        }

    }
}
